import { defineStore } from "pinia";
import { ref } from "vue";
import { getCartListAPI,postCartAPI,deleteCartAPI } from "@/apis/cart";
export const useCartStore = defineStore('cart',()=>{
  const cartList = ref([])

  // const getCartList = async() => {
  //   const res=await getCartListAPI()
  //   cartList.value=res.data.result
  // }
  const addCart = (goods) => {
    // getCartList()
    const item = cartList.value.find(item => item.skuId === goods.skuId)
    if(item){
     
      item.count++
    }else {
      cartList.value.push(goods)
    }
  }

  const delCart=async(id)=>{
    await deleteCartAPI([id])

  }
  return {cartList,addCart,delCart}
},
{
  persist: true,
}
)